package io.jboot.admin.controller.led;

import com.jeesuite.filesystem.FSProvider;
import com.jeesuite.filesystem.UploadObject;
import com.jeesuite.filesystem.provider.qiniu.QiniuProvider;
import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.upload.UploadFile;
import io.jboot.admin.base.rest.datatable.DataTable;
import io.jboot.admin.base.web.base.BaseController;
import io.jboot.admin.service.api.LogService;
import io.jboot.admin.service.entity.model.Log;
import io.jboot.admin.utils.QiniuUtils;
import io.jboot.admin.utils.RetCon;
import io.jboot.core.rpc.annotation.JbootrpcService;
import io.jboot.web.controller.annotation.RequestMapping;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.List;

/**
 * led互动
 *
 * @author Rlax
 */
@RequestMapping("/led/interact")
public class LedController extends BaseController {


    /**
     * index
     */
    public void index() {
        //setAttr("name", decode(getCookie("websocket_name")));
        render("main.html");
    }

    public void ledindex() {
        render("server.html");
    }

    /**
     * 表格数据
     */
    public void upload_file() {
        RetCon ret = new RetCon();

        List<UploadFile> files = getFiles();
        if (null == files || files.size() < 1) {
            ret.setFail();
            ret.setMsg("没有发现文件");
            renderJson(ret);
        } else {
            //默认处理单文件上传
            String url = QiniuUtils.upload(files.get(0));
            ret.setOk();
            ret.setMsg("文件上传成功!");
            ret.setObj(url);
            renderJson(ret);
        }
    }

    /***
     * 设置昵称
     */
    public void getwebsocket() {
        String host = getPara("host");
        String name = getPara("name");
        String ids = getCookie("websocket_ids");

        if (StrKit.isBlank(ids)) {
            ids = ":user:" + StrKit.getRandomUUID();
            setCookie("websocket_ids", ids, (3 * 365 * 24 * 60 * 60), true);
        }

        name = WebSocketController.setNameByIds(ids, name);
        setCookie("websocket_name", encode(name), (3 * 365 * 24 * 60 * 60), true);

        renderJson(Ret.ok("url", "ws://" + host + "/led/websocket?" + ids));
    }

    /***
     * 设置昵称
     */
    public void getwebsocket_led() {
        String host = getPara("host");
        String name = getPara("name");
        String idsled = getCookie("websocket_ids_led");

        if (StrKit.isBlank(idsled)) {
            idsled = ":led:" + StrKit.getRandomUUID();
            setCookie("websocket_ids_led", idsled, (3 * 365 * 24 * 60 * 60), true);
        }

        WebSocketController.setJiubaNameByIds(name);
        setCookie("websocket_name_led", encode(name), (3 * 365 * 24 * 60 * 60), true);

        renderJson(Ret.ok("url", "ws://" + host + "/led/websocket?" + idsled));
    }

    private String encode(String name) {
        if (StrKit.notBlank(name)) {
            try {
                return URLEncoder.encode(name, "utf-8");
            } catch (UnsupportedEncodingException e) {
            }
        }
        return null;
    }

    private String decode(String name) {
        if (StrKit.notBlank(name)) {
            try {
                return URLDecoder.decode(name, "utf-8");
            } catch (UnsupportedEncodingException e) {
            }
        }
        return null;
    }
}
